package dormouse.gui;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/**
 * This table provides alternating row colors.
 */
public class StripedTable extends JTable {

	/** Serial UID.*/
	private static final long serialVersionUID = -3646130187438018936L;
	
	/**
	 * The background color for the odd rows of the table.
	 */
	private Color oddRowColor = Color.decode("#F1F5FA");

	/**
	 * The background color for the even rows of the table.
	 */
	private Color evenRowColor = Color.WHITE;
	
	/**
	 * Constructor
	 */
	public StripedTable (){
		super();
	}
	
	/**
	 * Constructor
	 * @param oddRowColor	The background color for the odd rows of the table.
	 * @param evenRowColor	the background color for the even rows of the table.
	 */
	public StripedTable (Color oddRowColor, Color evenRowColor){
		super();
		this.oddRowColor = oddRowColor;
		this.evenRowColor  = evenRowColor;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
			int vColIndex) {
		Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
		if (rowIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
			c.setBackground(this.oddRowColor);
		} else {
			// If not shaded, match the table's background
			c.setBackground(this.evenRowColor);
		}
		return c;
	}
	

}
